package parser;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;

public class Yotc8000Parser implements IParser {

	@Override
	public void parse(String filePath) {
		
		/*格式如：				 
			莫城10.4.71.254(config-if-pon-6/2)#show mac-addr table
			Success getting address table from PON 2. Address table size : 244
			    Onu Id       LLID          Mac-Address       Type
			-------------------------------------------------------
			         0        127    00:0b:3d:01:1a:10    DYNAMIC
			        19          3    00:12:d6:35:2c:13    DYNAMIC
			         3          4    00:12:d6:35:36:47    DYNAMIC
			         8          8    00:12:d6:70:80:d0    DYNAMIC    
		--More--                    30         10    00:12:d6:3f:0f:54    DYNAMIC	         
		 */
		File file=new File(filePath);
		String olt = file.getName();
		olt = olt.substring(0,olt.indexOf(".log"));
		File file1=new File(filePath+".out");
		try {
			if(!file.exists()||file.isDirectory())
			    throw new FileNotFoundException();
			
			
			FileWriter fw=new FileWriter(file1);
			BufferedReader br=new BufferedReader(new FileReader(file));
			String temp=br.readLine();
			String[] onus;
			String pon="";	
			int id=1;
			while(temp!=null){
				if (temp.indexOf("config-if-pon-") > 0) {
					pon = temp.substring(temp.indexOf("pon-")+4,temp.indexOf(")#"));					
				}
				if (temp.indexOf("DYNAMIC") >0) {
					onus = temp.trim().split("\\s+"); //以n个空格切分
					if (temp.indexOf("More") > 0) {
						fw.write("id:"+id+",olt:"+olt+",eoc mac:"+onus[5]+",getOnuPort:" + pon + "/" + onus[3]+"\n");
					} else {
						fw.write("id:"+id+",olt:"+olt+",eoc mac:"+onus[2]+",getOnuPort:" + pon + "/" + onus[0]+"\n");
					}	
					id++;
				}	
			    temp=br.readLine();
			}
			br.close();
			fw.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("deal "+filePath+" error!" );
		} 
    }	
	
	/* 格式如：
	 * 莫城10.4.71.254(config)# show pon mac-addr configuration
			PON  1/1 :
			 Mac address table aging time  : 300 s
			 Mac address table limit       : 8191
			PON  1/2 :
			 Mac address table aging time  : 300 s
			 Mac address table limit       : 8191
	 */
	public Vector<String> preParse(String midData) {
		try{
			BufferedReader br = new BufferedReader(new InputStreamReader(
													new ByteArrayInputStream(midData.getBytes())
												   ));  
			String temp=br.readLine();
			String[] onu;
			String[] ids;
			Vector<String> retv = new Vector();			
			
			while(temp!=null){	
				if (temp.startsWith("PON")) {
					onu = temp.split("\\s+");
					String pon = onu[1];
					retv.add(pon);
				}	
			    temp=br.readLine();			    
			}
			br.close();
			return retv;
		}catch (Exception ex){
			ex.printStackTrace();
		}		
		return null;
	}
	
	public static void main(String[] args) {
		String temp = "????10.3.160.250(256)C8000(config-if-pon-1/1)#show mac-addr table";
		System.out.println(temp.indexOf("pon-"));
		System.out.println(temp.indexOf(")#"));
		String pon = temp.substring(temp.indexOf("pon-")+4,temp.indexOf(")#"));
		System.out.println(pon);
		//new Yotc8000Parser().parse("E:\\workspace\\javaTelenet\\bin\\output\\output\\10.3.160.250.log");

	}

}
